package com.wjq.house.web;


import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.CertAlipayRequest;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.wjq.house.config.ResultEntity;
import com.wjq.house.config.alibaba.PayConfig;
import com.wjq.house.entity.Order;
import com.wjq.house.entity.Record;
import com.wjq.house.service.IOrderService;
import com.wjq.house.service.IRecordService;
import com.wjq.pay.service.AlipayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
public class AlibabaPayContoller {


    @Autowired
    PayConfig payConfig;

    @Autowired
    RedisTemplate<String,String> redisTemplate;

    @Autowired
    AlipayService alipayService;
    @Autowired
    private IOrderService iOrderService;
    @Autowired
    private IRecordService iRecordService;

    @RequestMapping("/alibaba/pay")
    public ResultEntity pay(String orderNo) throws AlipayApiException {

        log.info("订单号:{}",orderNo);

        if (StringUtils.isNotBlank(redisTemplate.opsForValue().get(orderNo))){
            return ResultEntity.ok("支付二维码返回成功",redisTemplate.opsForValue().get(orderNo));
        }

        Order order = new Order();
        order.setOrderid(orderNo);
        order.setStatus("0");
        order.setCreatetime(new Date());
        iOrderService.insert(order);
        Record r =iRecordService.list(orderNo);
        CertAlipayRequest certAlipayRequest = new CertAlipayRequest();
        certAlipayRequest.setServerUrl(payConfig.getUrl());
        certAlipayRequest.setAppId(payConfig.getAppid());
        certAlipayRequest.setPrivateKey(payConfig.getAppPrivateKey());
        certAlipayRequest.setFormat("json");
        certAlipayRequest.setCharset("utf-8");
        certAlipayRequest.setSignType("RSA2");
        certAlipayRequest.setCertPath(payConfig.getAppCertPath());

        certAlipayRequest.setAlipayPublicCertPath(payConfig.getAlipayCertPath());
        certAlipayRequest.setRootCertPath(payConfig.getAlipayRootCertPath());
        DefaultAlipayClient alipayClient = new DefaultAlipayClient(certAlipayRequest);
//        AlipayClient alipayClient = new DefaultAlipayClient(payConfig.getUrl(), payConfig.getAppid(), payConfig.getAppPrivateKey(), "json", "utf-8", payConfig.getAlipayPublicKey(), "RSA2");

        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();//创建API对应的request类
        Map<String,String> maps = new HashMap();
        maps.put("out_trade_no",orderNo);
        maps.put("total_amount",r.getSs().toString());
        maps.put("subject","医疗费用");
        maps.put("store_id",r.getId().toString());
        maps.put("timeout_express","3600m");
        request.setBizContent(JSON.toJSONString(maps));
        //设置回调的url地址 关键
        request.setNotifyUrl(payConfig.getCallBackUrl());
        AlipayTradePrecreateResponse response = alipayClient.certificateExecute(request);
        System.err.print(response.getBody());
        String qrcode = response.getQrCode();
        //String qrcode=alipayService.getQrCode(orderNo);
        redisTemplate.opsForValue().set(orderNo,qrcode);
        return ResultEntity.ok("支付二维码返回成功",redisTemplate.opsForValue().get(orderNo));

    }
}